package com.cole.gx095.controller;


import com.cole.gx095.Repository.*;
import com.cole.gx095.domain.BillCorData;
import com.cole.gx095.domain.CostVo;
import com.cole.gx095.domain.mapper.CostVOMapper;
import com.cole.gx095.service.SaleBillService;
import com.cole.gx095.controller.utils.CommonRep;
import com.cole.gx095.controller.utils.RedisUtil;
import com.cole.gx095.entity.Correlation;
import com.cole.gx095.entity.Salebill;
import com.cole.gx095.entity.Salecontract;
import com.cole.gx095.entity.Stockgoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/billManage")
@CrossOrigin
@Transactional
public class billManageController {
    @Autowired
    SalebillRepository salebillRepository;
    @Autowired
    SalecontractRepository salecontractRepository;
    @Autowired
    SaleBillService saleBillService;
    @Autowired
    StockgoodsRepository stockgoodsRepository;
    @Autowired
    CorrelationRepository correlationRepository;
    @Autowired
    ArgumentRepository argumentRepository;

    @GetMapping("/List")
    CommonRep List(Integer currentPage, Integer pageSize){
        //0成本占用 1进入补票界面
        Page<Salebill> page1 = saleBillService.findAllByBillStatus(1, currentPage, pageSize);
        List<CostVo> costVoList = new ArrayList<>();
        for (Salebill salebill : page1.getContent()) {
            System.out.println("salebill = " + salebill);
            CostVo conver = CostVOMapper.INSTANCT.conver(salebill);
            conver.setId(salebill.getId());
            conver.setBuyUnit(salecontractRepository.findoneBycontractNum(salebill.getContractNum()).getBuyUnit());
            costVoList.add(conver);
        }
        costVoList.forEach(System.out::println);
        return CommonRep.ok()
                .data("page",page1)
                .data("List",costVoList);
    }

    /**
     * 操作库存表和关联表
     * 判断发票是否关联完成
     * @param billCorDataList
     * @return
     */
    @PostMapping("/addCor")
    CommonRep addCor(@RequestBody List<BillCorData> billCorDataList){
        Salebill salebill = salebillRepository.findoneBybillNum(billCorDataList.get(0).getSaleBillNum());

        for (BillCorData billCorData : billCorDataList) {
            Stockgoods byId = stockgoodsRepository.getById(billCorData.getStockgoodsId());
            byId.setCorrelationAccount(billCorData.getCorrelationAccount()+byId.getCorrelationAccount());
            byId.setRemainAccount(byId.getRemainAccount() - billCorData.getCorrelationAccount());
            stockgoodsRepository.save(byId);

            Correlation correlation = new Correlation();
            correlation.setSaleBillId(salebill.getId());
            correlation.setSaleBillNum(salebill.getBillNum());
            correlation.setSaleContractId(salebill.getContractId());
            correlation.setSaleContractNum(salebill.getContractNum());

            correlation.setPurchanseBillId(byId.getBillId());
            correlation.setPurchanseBillNum(byId.getBillNum());
            correlation.setPurchanseContractId(byId.getContractId());
            correlation.setPurchanseContractNum(byId.getContractNum());

            correlation.setStockGoodsId(billCorData.getStockgoodsId());
            correlation.setTypeName(billCorData.getTypeName());
            correlation.setModle(billCorData.getModle());
            correlation.setGoodsName(billCorData.getGoodsName());
            correlation.setGoodsUnitPrice(billCorData.getGoodsUnitPrice());

            correlation.setCorrelationAccount(billCorData.getCorrelationAccount());
            correlation.setCorrelationAmount(billCorData.getCorrelationAccount()*billCorData.getGoodsUnitPrice());
            correlation.setCreateAdminTrueName(RedisUtil.mget("LoginName"));
            correlation.setCorrelationType(1);//0成本占用 1补票占用
            correlation.setStatus(0);//0正常 1作废
            correlationRepository.save(correlation);
        }
        //计算关联率 并将关联金额和关联率存到salebill表
        Double billcorrelationAmount = 0.0;
        List<Correlation> correlationList = correlationRepository.findByBillId(salebill.getId());
        for (Correlation correlation : correlationList) {
            if(correlation.getStatus()==0)//判断关联是否生效
                billcorrelationAmount += correlation.getCorrelationAmount();
        }
        salebill.setCorrelationAmount(billcorrelationAmount);
        salebill.setCorrelationRate(billcorrelationAmount / salebill.getBillAccount() * 100);
        if(salebill.getCorrelationRate() > argumentRepository.getById(1).getValue()){//判断是否发票关联完成
            salebill.setStatus(1);
        }
        salebillRepository.save(salebill);

        //更新发票所关联的销售合同关联率
        Double contractCorAmount = salebillRepository.sumAmountBycontractNum(salebill.getContractNum());
        Salecontract salecontract = salecontractRepository.findoneBycontractNum(salebill.getContractNum());
        salecontract.setCorrelationAmount(contractCorAmount);
        salecontract.setCorrelationRate(contractCorAmount / salecontract.getContractAmount() * 100);
        if(salecontract.getCorrelationRate() > argumentRepository.getById(1).getValue()){
            salecontract.setStatus(1);
        }
        salecontractRepository.save(salecontract);
        return CommonRep.ok();
    }


}
